/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modlemgooglesvn.modlem_src;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author xXx
 */

public class BinaryTree {

//  public static void main(String[] args) throws IOException
//
//    {
//  new BinaryTree().run();
//  }
    static class Node {

        Node left;
        Node right;
        Double value;
        int numOfObj;
        int numAtt;
//        String sign;
        boolean leaf;
        File input;
        Node parent;
        
        public Node getParent(){return parent;}
        public Node getLeftChild(){return left;}
        public Node getRightChild(){return right;}
        
        public Node(Node parent,Double value, int numOfObj, int numAtt, File input, boolean leaf) {
            this.value = value;
            this.numOfObj = numOfObj;
//            this.sign = sign;
            this.input = input;
            this.numAtt = numAtt;
            this.parent = parent;
            this.leaf = leaf;
        }
    }
    protected String whatSplits;
    protected File rootInput;
    protected File rootOutput;
    protected int numOfRecord;
    protected File parameters;
    
    public void setParameters(File parameters){this.parameters=parameters;}
    public File getParameters(){return parameters;}
    

    public void setNumOfRecord(int numOfRecord) {

        this.numOfRecord = numOfRecord;

    }

    public int getNumOfRecord() {

        return numOfRecord;

    }

    public File getRootInput() {
        if (rootInput == null) {
            System.out.println("Pusty rootInput");
            return null;
        }
        return rootInput;
    }

    public File getRootOutput() {

        if (rootOutput == null) {
            System.out.println("Pusty rootOutput");
            return null;
        }

        return rootOutput;

    }

    public void setRootInput(File rootSource) {

        this.rootInput = rootSource;

    }

    public void setRootOutput(File rootSource) {

        this.rootOutput = rootSource;

    }

    public void setWhatSplits(String whatSplits) {

        this.whatSplits = whatSplits;

    }

    public String getWhatSplits() {

        return this.whatSplits;
    }

    public void run() throws FileNotFoundException, IOException {
//  Node rootnode = new Node(25);
        Double rootValue = 0.0;
        int rootNumOfObj = 0;
        int rootNumAtt = 0;
        String rootSign = "";
        int i = 1;
        NajlepszyPodzial np = new NajlepszyPodzial();
        PodzialBinarny pb = new PodzialBinarny();
        KonwersjaBinarna kb = new KonwersjaBinarna();
        kb.konwersjaBazy(getRootInput(), getRootOutput(), getWhatSplits());
        
        this.parameters = kb.getParameters();
//        np.najlepszyPodzial();
        pb.podzialBinarny(rootInput, this.parameters, rootOutput, rootOutput, np.najlepszyPodzial);
        
//         DrzewoBinarne hd = new DrzewoBinarne();
            Node rootNode = new Node(null,rootValue, rootNumOfObj, rootNumAtt, rootInput,false);
      //  insert(rootnode, Integer.parseInt(pb.jakPodzielilo[0]),Integer.parseInt(pb.jakPodzielilo[1]));
        insert(rootNode);
        
        

//              hd.ustawZrodlo(output);
//              hd.umiescWartosc(null, parametry);      

    
        
//
//        System.out.println("Building tree with rootvalue" + rootnode.value);
//        System.out.println("=================================");
//  
//        System.out.println("Traversing tree in order");
//        System.out.println("=================================");
        printInOrder(rootNode);

    }
int i = 0;
    public void insert(Node node) throws IOException {
        
        node.parent=node;
        if(node.leaf!=true){
        NajlepszyPodzial np = new NajlepszyPodzial();
        PodzialBinarny pb = new PodzialBinarny();
        pb.podzialBinarny(node.input, parameters, new File(node.input.getAbsolutePath()+i++),new File(node.input.getAbsolutePath()+i++), np.najlepszyPodzial);
        
        
        
//        int value = pb.podzialBinarny(rootInput, this.parameters, rootOutput, rootOutput, np.najlepszyPodzial)[1];
//        int numAtt = pb.podzialBinarny(rootInput, this.parameters, rootOutput, rootOutput, np.najlepszyPodzial)[0];
        int numOfObjL = pb.jakPodzielilo[0];
        int numOfObjR = pb.jakPodzielilo[1];
        
        Double value = Double.parseDouble(np.najlepszyPodzial[1]);
        int numAtt = Integer.parseInt(np.najlepszyPodzial[0]);
        
        node.left = new Node(node.left,value, numOfObjL,  numAtt, source);
        node.right = new Node(value, numOfObjR, numAtt, source);
        
        if (node.left != null) {
                insert(node.left, value, numAtt, numOfObj, source);
           
//                System.out.println("  Inserted " + value
//                        + " to left of node " + node.value);
                 }
                
                
//                insert(node.parent,value, numOfObj, numAtt, sign, source);

            
       
            if (node.right != null) {
                insert(node.right, value,  numAtt,numOfObj, sign, source);
            } 
//                System.out.println("  Inserted " + value + " to right of node " + node.value);
                
              //  node.parent=node;
//                insert (node.right,value, numOfObj, numAtt, sign, source);
                
            
        
        
        
        }
        insert(node.parent.right);
    }

    public void printInOrder(Node node) {
        if (node != null) {
            printInOrder(node.left);
            System.out.println("  Traversed " + node.value);
            printInOrder(node.right);
        }
    }
}
